package xzy.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
import xzy.dao.StudentMapper;
import xzy.pojo.Student;

import javax.servlet.http.HttpSession;
import java.io.IOException;

public class Login_Servlet extends javax.servlet.http.HttpServlet {
    private static ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("学生端登录");
        this.doPost(request, response);
        StudentMapper studentMapper= (StudentMapper) context.getBean("studentMapper");
        String userName=request.getParameter("userName");
        String password=request.getParameter("password");
        request.setAttribute("userName",userName);
        request.setAttribute("password",password);
        try{
            if(!StringUtils.isEmpty(userName)&&!StringUtils.isEmpty(password)) {
                Student student=studentMapper.findStudentByName(userName);
                if(student!=null && student.getPassword().equals(password)){
//                客户端跳转
                    HttpSession session=request.getSession();
                    session.setAttribute("user",userName);
                    request.setAttribute("error","");
                    request.getRequestDispatcher("/Student-info2.jsp").forward(request,response);
                }else {
                    request.setAttribute("error","用户名不存在或者密码错误");
                    request.getRequestDispatcher("/index3.jsp").forward(request,response);
                }
            }else {
                request.setAttribute("error","用户名或者密码为空");
                request.getRequestDispatcher("/index3.jsp").forward(request,response);
            }
        }catch (Exception e){
            request.setAttribute("error","用户名，密码不可以为空");
            request.getRequestDispatcher("/index3.jsp").forward(request,response);
        }finally {

        }
    }
}
